var page;//完工列表的页码
var _index = -1;//完工表的下标（用于表勾选）
var rowIndex = "%^%%%%";//完工表的下标（用于改变选中行样式）
var flag = false;//区分状态
var fWorker = "$#^$%%^$%^$%";
var fNumberData,fOperSNData;
var input_name = ["工序","良品","工废","料废","工废1","返修","次品"];
var input_id = ["fOper","fPassQty","fScrapQty","fForitemQty","fScrapNotMeQty","fReworkQty","fOthQty"];
var input_sum = -1;
var fIsMerge = 0;//表示是否是合并工序,1--合并，0--不合并
var fDisPathId;
var fStartWorkId;
// 保存表格刷新时的页码
var currentPageNumber;
/*
 * @Description: 预加载
 * @param : 
 */
$(function() {
	resizeIframeHight();
	initFinishTable();
	$("#fWorkerF").focus();
	baoname();
});

/** 
 * @description: 初始化开工记录表 
 * @param :  
 */
function initFinishTable() {
	$('#finishTable').bootstrapTable('destroy').bootstrapTable({
		url : pathUrl("start/findByCondition.action"),//请求后台的URL
		//height : $(window).height() - 200,//行高
		method : "POST",//请求方式
		//striped : true,//是否显示行间隔色
		cache : false,//是否使用缓存，默认为true
		pagination : true,//是否显示分页
		clickToSelect : true,//是否启用点击选中行
		singleSelect : true,//是否单选
		pageNumber : 1,//初始化加载第几页
		pageSize : 10,//每页的记录行数
		sidePagination : "server", // 分页方式：client客户端分页，server服务端分页（*）
		pageList : [ 10, 25, 50, 100 ],// 分页方式：client客户端分页，server服务端分页
		rowStyle : function(row, index) {//自定义样式，参数为：row：行数据，index：行下标
			var classes = [ 'success' ];
			if (index == rowIndex) {
				return {
					classes : classes[0]
				};
			}
			return {};
		},
		queryParams : function(params) {//传递参数
			var data= {};
			data.offset = params.offset;
			data.row = params.limit;
//			data.fWorker = $("#fWorkerF").val() == ""?"$#^$%%^$%^$%":$("#fWorkerF").val();
			//data.sortOrder = params.order;//排序
			//data.sortName = params.sort;//排序
			if(fWorker.substring(0,2) == "PG" || fWorker.substring(0,3) == "MPG"){
//				data.fCode = $("#fWorker").val();
				data.fCode = fWorker;
			}else{
				data.fWorker = fWorker;
			}
			data.fIsBgt = 1;
			return data;
		},
		dataType : 'JSON',//数据类型
		uniqueId : "id",//每一行的唯一标识，一般为主键列
		columns : [/* {
			checkbox : true
		}, */{
			field : "fBillNo",
			align : 'left',
			valign: 'middle',
			title : '任务单号'
		},{
			field : "fCode",
			align : 'left',
			valign: 'middle',
			title : '派工单号'
		}/*, {
			field : "fNumber",
			align : 'left',
			valign: 'middle',
			title : '产品代码'
		}*/, {
			field : "fName",
			align : 'left',
			valign: 'middle',
			title : '产品名称'
		}, {
			field : "fModel",
			align : 'left',
			valign: 'middle',
			title : '规格型号',
			formatter : function(value,row,index){
				return '<a href="javascript:void(0);" class="ggxh">'+value+'</a>';
			},
			events : operateEvents
		}, {
			field : "fOperSN",
			align : 'left',
			valign: 'middle',
			title : '工序号'
		}, {
			field : "fOper",
			align : 'left',
			valign: 'middle',
			title : '工序名称',
			formatter : formatterStringShort
		}, {
			field : "fQcStatus",
			align : 'left',
			valign: 'middle',
			sortable : true,
			title : '首检',
			formatter : function(value,row,index){
				if(row.fQcStatus > 1){
					return "是";
				}else if(row.fQcStatus <= 1){
					return "否";
				}
			}
		}, {
			field : "fNote",
			align : 'left',
			valign: 'middle',
			title : '加工说明',
			formatter : formatterStringLong
		}, {
			field : "fMachine",
			align : 'left',
			valign: 'middle',
			title : '设备名称'
		}, {
			field : "fTargetQty1",
			align : 'left',
			valign: 'middle',
			title : '派工数量',
			formatter : function(value,row,index){
				return parseFloat(row.fTargetQty) - parseFloat(row.fQty);
			}
		}, {
			field : "fWorkerName",
			align : 'left',
			valign: 'middle',
			title : '操作工'
		},  {
			field : "fDate",
			align : 'left',
			valign: 'middle',
			title : '开工时间',
			formatter : function(value,row,index){
				return timeFormatter(row.fDate);
			}
		}],
		onClickRow : function(row, $element) {//行单击
//			console.log(row)
			$('.success').removeClass('success');// 去除之前选中的行的，选中样式
			$($element).addClass('success');// 添加当前选中的 success样式用于区别
			index = $element.data('index');
			rowIndex = index;
			fDisPathId = row.fDisPathId;
			fStartWorkId = row.id;
			page = $('.pagination .active').find("a").eq(0).text();
			$("#content_bottom_single").css("display","none");
			$("#contentId_single").html("");
			$("#contentId").html("");
			loadingCustom('正在加载中。。。');
			if($element.context.getElementsByClassName("ggxh").length > 0){
				swal.close();//关闭sweetAlert2提示框
			}else{
				var fNumberSub = row.fNumber.substr(0,2);			
				if(fNumberSub == "04" || (fNumberSub == "99" && row.fNumber.substr(3,2) != "WY")){
					if(row.fQcStatus <= 1){
						// swal_Tips("该条记录还未进行首检！");
						showSwal('挂起', '该条记录未首检，不可报工！如需进行挂起操作，请点击挂起', '挂起', function(result) {
							if (result.value) {
								// TODO:添加挂起操作事件
								showLoading();
								$.ajax({
									url : pathUrl("end/suspending.action"),
									type : "POST",
									dataType : "JSON",
									data : {
										fDisId : row.fDisPathId,
										fStartWorkId: row.id,
									},
									success : function(data) {
										if(data.code == "0000"){
											swal_Tips_Short(data.message);
											$('#finishTable').bootstrapTable('refresh');
										}else{
											swal_Tips(data.message);
										}
									},
									error : function(request) {
										swal_Tips_Error();
									},
								});
							}
						});
						return;
					}	
				}	
				
				if(row.fIsMerge == 1){
					fIsMerge = 1;
					$.ajax({
						url : pathUrl("start/findByMergeOper.action"),
						type : "POST",
						dataType : "JSON",
						data : {
							fQty : row.fTargetQty,
							id : row.id
						},
						success : function(data) {						
//							console.log(data);
							input_sum = data.length;
							$("#modal-body-id").css("height",data.length * 40 + 70);
							if(data.length  > 0){
								//循环生产form表单
								var hl = "";
								for(var i = 0; i < data.length; i++){
									var temp = data[i];	
									hl += "<div style='float:left;width: 104%;'><span id = 'fOperSN" + i + "'style = 'display:none;'>" + temp.fOperSN + "</span><span id = 'fMergeCode" + i + "'style = 'display:none;'>" + temp.fMergeCode + "</span>";
									hl += "<span id = 'fCardId" + i + "'style = 'display:none;'>" + temp.fCardId + "</span>";
									for(var j = 0; j < input_id.length; j++){
										if(j == 4){
											hl += "<div class='col-sm-2 col-md-2 bottomContent' style='display:none;width: 14%; padding:0px 0px 3px 0px;'><span style= 'display:none;' class='bottomSpan'>";
										}else{
											hl += "<div class='col-sm-2 col-md-2 bottomContent' style='width: 14%; padding:0px 0px 3px 0px;'><span class='bottomSpan'>";
										}																	
		                                if(j == 1){
											hl += "<span class='requiredCss'>*</span>" + input_name[j] + ":";
										}else{
											hl += input_name[j] + ":";
										}
										hl += "</span>";
										var valu = temp[input_id[j]];
										if(valu == null){
											valu = 0;
										}
										if(j == 0){
											hl += "<input type='text' id='" + input_id[j] + i +"' readonly = 'true' value = '" + valu + "'";
										}else if(j == 1){//当数量为良品数的时候	
											var t = parseFloat(row.fTargetQty) - parseFloat(row.fQty);
											hl += "<input type='text' id='" + input_id[j] + i +"'value = '" + t +"'";	
										}else if(j == 4){
											hl += "<input type='text' style= 'display:none;' id='" + input_id[j] + i +"'value = '" + valu +"'";
										}else{
											hl += "<input type='text' id='" + input_id[j] + i +"'value = '" + valu +"'";
										}								
										hl += " name='" + input_id[j] + "' class='clean-c form-control bj_color' autocomplete='off' onkeyup= changeVal(this) onafterpaste='if(isNaN(value))execCommand(&apos;undo&apos;)'/>";															
										hl += "</div>";										
									}	
									hl += "</div>";
								}
								$("#contentId").html(hl);
								$("#content_bottom").css("display","block");
								$('#fPassQty0').focus();//良品数输入框获得焦点
								$("#mergeModal").modal('show');
								swal.close();//关闭sweetAlert2提示框
							}else{
								input_sum = 1;
								var hl = "";
								for(var i = 0; i < input_name.length; i++){	
									if(i == 4){
										hl += "<div class='col-sm-2 col-md-2 bottomContent' style='display:none;width: 14%; padding:0px 0px 3px 0px;'><span class='bottomSpan'>";
									}else{
										hl += "<div class='col-sm-2 col-md-2 bottomContent' style='width: 14%; padding:0px 0px 3px 0px;'><span class='bottomSpan'>";
									}															
				                    if(i == 1){
										hl += "<span class='requiredCss'>*</span>" + input_name[i] + ":";
									}else{
										hl += input_name[i] + ":";
									}
									hl += "</span>";
									if(i == 0){
										hl += "<input type='text' id='" + input_id[i] + 0 +"' readonly = 'true'";
									}else{
										hl += "<input type='text' id='" + input_id[i] + 0 +"'";	
									}								
									hl += " name='" + input_id[i] + "' class='clean-c form-control bj_color' autocomplete='off' onkeyup='if(isNaN(value))execCommand(&apos;undo&apos;)' onafterpaste='if(isNaN(value))execCommand(&apos;undo&apos;)'/>";															
									hl += "</div>";
								}
								$("#contentId_single").html(hl);
								$("#fPassQty0").val(parseFloat(row.fTargetQty) - parseFloat(row.fQty));//给良品数赋值	
								$("#fOper0").val(row.fOper);//给良品数赋值	
								$('#fPassQty0').focus();//良品数输入框获得焦点
								$("#content_bottom_single").css("display","block");
								swal.close();//关闭sweetAlert2提示框
							}					
						},
						error : function(request) {
							swal_Tips_Error();
						}
					});
				}else{
					fIsMerge = 0;
					input_sum = 1;
					var hl = "";
					for(var i = 0; i < input_name.length; i++){	
						if(i == 4){
							hl += "<div class='col-sm-2 col-md-2 bottomContent' style='width: 14%; padding:0px 0px 3px 0px;display:none;'><span class='bottomSpan'>";	
						}else{
							hl += "<div class='col-sm-2 col-md-2 bottomContent' style='width: 14%; padding:0px 0px 3px 0px;'><span class='bottomSpan'>";
						}													
	                    if(i == 1){
							hl += "<span class='requiredCss'>*</span>" + input_name[i] + ":";
						}else{
							hl += input_name[i] + ":";
						}
						hl += "</span>";
						if(i == 0){
							hl += "<input type='text' id='" + input_id[i] + 0 +"' readonly = 'true'";
						}else{
							hl += "<input type='text' id='" + input_id[i] + 0 +"'";	
						}								
						hl += " name='" + input_id[i] + "' class='clean-c form-control bj_color' autocomplete='off' onkeyup='if(isNaN(value))execCommand(&apos;undo&apos;)' onafterpaste='if(isNaN(value))execCommand(&apos;undo&apos;)'/>";															
						hl += "</div>";
					}
					$("#contentId_single").html(hl);
					$("#fPassQty0").val(parseFloat(row.fTargetQty) - parseFloat(row.fQty));//给良品数赋值	
					$("#fOper0").val(row.fOper);//给良品数赋值	
					$('#fPassQty0').focus();//良品数输入框获得焦点
					$("#content_bottom_single").css("display","block");	
					swal.close();//关闭sweetAlert2提示框
				}	
			}			
			$("#fWorker").val(row.fWorker);//获取操作工ID
			$("#fStartWorkId").val(row.id);//获取开工表ID
			$("#fTargetQty").val(parseFloat(row.fTargetQty) - parseFloat(row.fQty));//获取派工数
//			$("#fPassQty").val(parseFloat(row.fTargetQty) - parseFloat(row.fQty));//给良品数赋值
//			$('#fPassQty').focus();//良品数输入框获得焦点			
		},
		onPageChange : function(number, size) {//翻页
			if (page == number) {
				rowIndex = index;
			} else {
				$('.success').removeClass('success');
				rowIndex = "%^%%%%";
			}
		},
		onUncheck : function(index){//取消勾选
			$("#fWorker,#fStartWorkId,#fTargetQty").val("");//清空操作工ID、开工表ID、派工数
			$('#fPassQty').blur();//良品数输入框失去焦点
			$('.success').removeClass('success');
			rowIndex = "%^%%%%";
		},
		onLoadSuccess : function(data){
			$("#fWorkerF").val("");
			$("#fWorkerF").focus();
		}
	});
}

/** 
 * @description: 刷新后，重新初始化开工记录表并停留在当前页面 
 * @param :  当前页码 currentPageNumber
 */
function initFinishTableWithCurrentPage(currentPageNumber) {
	$('#finishTable').bootstrapTable('destroy').bootstrapTable({
		// 请求后台的URL
		url: pathUrl("start/findByCondition.action"),
		// 行高
		//height : $(window).height() - 200,
		// 请求方式
		method: "POST",
		// 是否显示行间隔色
		//striped : true,
		// 是否使用缓存，默认为true
		cache: false,
		// 是否显示分页
		pagination: true,
		// 是否启用点击选中行
		clickToSelect: true,
		// 是否单选
		singleSelect: true,
		// 初始化加载第几页
		pageNumber: currentPageNumber,
		// 每页的记录行数
		pageSize: 10,
		// 分页方式：client客户端分页，server服务端分页（*）
		sidePagination: "server", 
		// 每页行数选项
		pageList : [ 10, 25, 50, 100 ],
		// 自定义样式，参数为：row：行数据，index：行下标
		rowStyle : function(row, index) {
			var classes = [ 'success' ];
			if (index == rowIndex) {
				return {
					classes : classes[0]
				};
			}
			return {};
		},
		queryParams : function(params) {//传递参数
			var data= {};
			data.offset = params.offset;
			data.row = params.limit;
//			data.fWorker = $("#fWorkerF").val() == ""?"$#^$%%^$%^$%":$("#fWorkerF").val();
			//data.sortOrder = params.order;//排序
			//data.sortName = params.sort;//排序
			if(fWorker.substring(0,2) == "PG" || fWorker.substring(0,3) == "MPG"){
//				data.fCode = $("#fWorker").val();
				data.fCode = fWorker;
			}else{
				data.fWorker = fWorker;
			}
			data.fIsBgt = 1;
			return data;
		},
		dataType : 'JSON',//数据类型
		uniqueId : "id",//每一行的唯一标识，一般为主键列
		columns : [/* {
			checkbox : true
		}, */{
			field : "fCode",
			align : 'left',
			valign: 'middle',
			title : '派工单号'
		}/*, {
			field : "fNumber",
			align : 'left',
			valign: 'middle',
			title : '产品代码'
		}*/, {
			field : "fName",
			align : 'left',
			valign: 'middle',
			title : '产品名称'
		}, {
			field : "fModel",
			align : 'left',
			valign: 'middle',
			title : '规格型号',
			formatter : function(value,row,index){
				return '<a href="javascript:void(0);" class="ggxh">'+value+'</a>';
			},
			events : operateEvents
		}, {
			field : "fOperSN",
			align : 'left',
			valign: 'middle',
			title : '工序号'
		}, {
			field : "fOper",
			align : 'left',
			valign: 'middle',
			title : '工序名称',
			formatter : formatterStringShort
		}, {
			field : "fQcStatus",
			align : 'left',
			valign: 'middle',
			sortable : true,
			title : '首检',
			formatter : function(value,row,index){
				if(row.fQcStatus > 1){
					return "是";
				}else if(row.fQcStatus <= 1){
					return "否";
				}
			}
		}, {
			field : "fNote",
			align : 'left',
			valign: 'middle',
			title : '加工说明',
			formatter : formatterStringLong
		}, {
			field : "fMachine",
			align : 'left',
			valign: 'middle',
			title : '设备名称'
		}, {
			field : "fTargetQty1",
			align : 'left',
			valign: 'middle',
			title : '派工数量',
			formatter : function(value,row,index){
				return parseFloat(row.fTargetQty) - parseFloat(row.fQty);
			}
		}, {
			field : "fWorkerName",
			align : 'left',
			valign: 'middle',
			title : '操作工'
		},  {
			field : "fDate",
			align : 'left',
			valign: 'middle',
			title : '开工时间',
			formatter : function(value,row,index){
				return timeFormatter(row.fDate);
			}
		}],
		onClickRow : function(row, $element) {//行单击
//			console.log(row)
			$('.success').removeClass('success');// 去除之前选中的行的，选中样式
			$($element).addClass('success');// 添加当前选中的 success样式用于区别
			index = $element.data('index');
			rowIndex = index;
			fDisPathId = row.fDisPathId;
			fStartWorkId = row.id;
			page = $('.pagination .active').find("a").eq(0).text();
			$("#content_bottom_single").css("display","none");
			$("#contentId_single").html("");
			$("#contentId").html("");
			loadingCustom('正在加载中。。。');
			if($element.context.getElementsByClassName("ggxh").length > 0){
				swal.close();//关闭sweetAlert2提示框
			}else{
				var fNumberSub = row.fNumber.substr(0,2);
				if(fNumberSub == "04" || (fNumberSub == "99" && row.fNumber.substr(3,2) != "WY")){
					if(row.fQcStatus <= 1){
						// swal_Tips("该条记录还未进行首检！");
						showSwal('挂起', '该条记录未首检，不可报工！如需进行挂起操作，请点击挂起', '挂起', function(result) {
							if (result.value) {
								// TODO:添加挂起操作事件
								showLoading();
								$.ajax({
									url : pathUrl("end/suspending.action"),
									type : "POST",
									dataType : "JSON",
									data : {
										fDisId : row.fDisPathId,
										fStartWorkId: row.id,
									},
									success : function(data) {
										if(data.code == "0000"){
											swal_Tips_Short(data.message);
											$('#finishTable').bootstrapTable('refresh');
										}else{
											swal_Tips(data.message);
										}
									},
									error : function(request) {
										swal_Tips_Error();
									}
								});
							}
						});
						return;
					}	
				}	
				if(row.fIsMerge == 1){
					fIsMerge = 1;
					$.ajax({
						url : pathUrl("start/findByMergeOper.action"),
						type : "POST",
						dataType : "JSON",
						data : {
							fQty : row.fTargetQty,
							id : row.id
						},
						success : function(data) {						
//							console.log(data);
							input_sum = data.length;
							$("#modal-body-id").css("height",data.length * 40 + 70);
							if(data.length  > 0){
								//循环生产form表单
								var hl = "";
								for(var i = 0; i < data.length; i++){
									var temp = data[i];	
									hl += "<div style='float:left;width: 104%;'><span id = 'fOperSN" + i + "'style = 'display:none;'>" + temp.fOperSN + "</span><span id = 'fMergeCode" + i + "'style = 'display:none;'>" + temp.fMergeCode + "</span>";
									hl += "<span id = 'fCardId" + i + "'style = 'display:none;'>" + temp.fCardId + "</span>";
									for(var j = 0; j < input_id.length; j++){
										if(j == 4){
											hl += "<div class='col-sm-2 col-md-2 bottomContent' style='display:none;width: 14%; padding:0px 0px 3px 0px;'><span style= 'display:none;' class='bottomSpan'>";
										}else{
											hl += "<div class='col-sm-2 col-md-2 bottomContent' style='width: 14%; padding:0px 0px 3px 0px;'><span class='bottomSpan'>";
										}																	
		                                if(j == 1){
											hl += "<span class='requiredCss'>*</span>" + input_name[j] + ":";
										}else{
											hl += input_name[j] + ":";
										}
										hl += "</span>";
										var valu = temp[input_id[j]];
										if(valu == null){
											valu = 0;
										}
										if(j == 0){
											hl += "<input type='text' id='" + input_id[j] + i +"' readonly = 'true' value = '" + valu + "'";
										}else if(j == 1){//当数量为良品数的时候	
											var t = parseFloat(row.fTargetQty) - parseFloat(row.fQty);
											hl += "<input type='text' id='" + input_id[j] + i +"'value = '" + t +"'";	
										}else if(j == 4){
											hl += "<input type='text' style= 'display:none;' id='" + input_id[j] + i +"'value = '" + valu +"'";
										}else{
											hl += "<input type='text' id='" + input_id[j] + i +"'value = '" + valu +"'";
										}								
										hl += " name='" + input_id[j] + "' class='clean-c form-control bj_color' autocomplete='off' onkeyup= changeVal(this) onafterpaste='if(isNaN(value))execCommand(&apos;undo&apos;)'/>";															
										hl += "</div>";										
									}	
									hl += "</div>";
								}
								$("#contentId").html(hl);
								$("#content_bottom").css("display","block");
								$('#fPassQty0').focus();//良品数输入框获得焦点
								$("#mergeModal").modal('show');
								swal.close();//关闭sweetAlert2提示框
							}else{
								input_sum = 1;
								var hl = "";
								for(var i = 0; i < input_name.length; i++){	
									if(i == 4){
										hl += "<div class='col-sm-2 col-md-2 bottomContent' style='display:none;width: 14%; padding:0px 0px 3px 0px;'><span class='bottomSpan'>";
									}else{
										hl += "<div class='col-sm-2 col-md-2 bottomContent' style='width: 14%; padding:0px 0px 3px 0px;'><span class='bottomSpan'>";
									}															
				                    if(i == 1){
										hl += "<span class='requiredCss'>*</span>" + input_name[i] + ":";
									}else{
										hl += input_name[i] + ":";
									}
									hl += "</span>";
									if(i == 0){
										hl += "<input type='text' id='" + input_id[i] + 0 +"' readonly = 'true'";
									}else{
										hl += "<input type='text' id='" + input_id[i] + 0 +"'";	
									}								
									hl += " name='" + input_id[i] + "' class='clean-c form-control bj_color' autocomplete='off' onkeyup='if(isNaN(value))execCommand(&apos;undo&apos;)' onafterpaste='if(isNaN(value))execCommand(&apos;undo&apos;)'/>";															
									hl += "</div>";
								}
								$("#contentId_single").html(hl);
								$("#fPassQty0").val(parseFloat(row.fTargetQty) - parseFloat(row.fQty));//给良品数赋值	
								$("#fOper0").val(row.fOper);//给良品数赋值	
								$('#fPassQty0').focus();//良品数输入框获得焦点
								$("#content_bottom_single").css("display","block");
								swal.close();//关闭sweetAlert2提示框
							}					
						},
						error : function(request) {
							swal_Tips_Error();
						}
					});
				}else{
					fIsMerge = 0;
					input_sum = 1;
					var hl = "";
					for(var i = 0; i < input_name.length; i++){	
						if(i == 4){
							hl += "<div class='col-sm-2 col-md-2 bottomContent' style='width: 14%; padding:0px 0px 3px 0px;display:none;'><span class='bottomSpan'>";	
						}else{
							hl += "<div class='col-sm-2 col-md-2 bottomContent' style='width: 14%; padding:0px 0px 3px 0px;'><span class='bottomSpan'>";
						}													
	                    if(i == 1){
							hl += "<span class='requiredCss'>*</span>" + input_name[i] + ":";
						}else{
							hl += input_name[i] + ":";
						}
						hl += "</span>";
						if(i == 0){
							hl += "<input type='text' id='" + input_id[i] + 0 +"' readonly = 'true'";
						}else{
							hl += "<input type='text' id='" + input_id[i] + 0 +"'";	
						}								
						hl += " name='" + input_id[i] + "' class='clean-c form-control bj_color' autocomplete='off' onkeyup='if(isNaN(value))execCommand(&apos;undo&apos;)' onafterpaste='if(isNaN(value))execCommand(&apos;undo&apos;)'/>";															
						hl += "</div>";
					}
					$("#contentId_single").html(hl);
					$("#fPassQty0").val(parseFloat(row.fTargetQty) - parseFloat(row.fQty));//给良品数赋值	
					$("#fOper0").val(row.fOper);//给良品数赋值	
					$('#fPassQty0').focus();//良品数输入框获得焦点
					$("#content_bottom_single").css("display","block");	
					swal.close();//关闭sweetAlert2提示框
				}	
			}			
			$("#fWorker").val(row.fWorker);//获取操作工ID
			$("#fStartWorkId").val(row.id);//获取开工表ID
			$("#fTargetQty").val(parseFloat(row.fTargetQty) - parseFloat(row.fQty));//获取派工数
//			$("#fPassQty").val(parseFloat(row.fTargetQty) - parseFloat(row.fQty));//给良品数赋值
//			$('#fPassQty').focus();//良品数输入框获得焦点			
		},
		onPageChange : function(number, size) {//翻页
			if (page == number) {
				rowIndex = index;
			} else {
				$('.success').removeClass('success');
				rowIndex = "%^%%%%";
			}
		},
		onUncheck : function(index){//取消勾选
			$("#fWorker,#fStartWorkId,#fTargetQty").val("");//清空操作工ID、开工表ID、派工数
			$('#fPassQty').blur();//良品数输入框失去焦点
			$('.success').removeClass('success');
			rowIndex = "%^%%%%";
		},
		onLoadSuccess : function(data){
			$("#fWorkerF").val("");
			$("#fWorkerF").focus();
		}
	});
}



/**
 * @description:“挂起”按钮点击事件
 * 
 */
$("#godown_single").click(function(){
	$("#godown").click();
});

/**
 * @description:“挂起”按钮点击事件
 */
$('#godown').click(function(){
	showLoading();
	$.ajax({
		url : pathUrl("end/suspending.action"),
		type : "POST",
		dataType : "JSON",
		data : {
			fDisId : fDisPathId,
			fStartWorkId: fStartWorkId,
		},
		success : function(data) {
			if(data.code == "0000"){
				swal_Tips_Short(data.message);
				$('#finishTable').bootstrapTable('refresh');
				$("#mergeModal").modal('hide');	
			}else{
				swal_Tips(data.message);
			}
		},
		error : function(request) {
			swal_Tips_Error();
		}
	});
})
/*
 * @Description: 提交按钮单击事件
 * @param :
 */
$("#save").click(function() {
	currentPageNumber = $('#finishTable').bootstrapTable('getOptions').pageNumber;
	// console.log($('#finishTable').bootstrapTable('getOptions'));
	// console.log(currentPageNumber);
	var ID =  $("#fStartWorkId").val();// 获取开工表ID
	var qty = parseFloat($("#fTargetQty").val());// 获取派工数
	for(var j = 0; j < input_sum; j++){
		var sum = 0;
		for(var i = 1; i < input_id.length; i++){
			/*if(i != 5){
				sum += parseFloat($("#" + input_id[i] + j).val() == "" ? 0 : $("#"+ input_id[i] + j).val());	
			}*/
			sum += parseFloat($("#" + input_id[i] + j).val() == "" ? 0 : $("#"+ input_id[i] + j).val());
		}
		if(sum < 1){
			swal_Tips("加工数量不能为0，注：加工数量=良品数+工废数+料废数+工废1+返修数+次品数！");
			return false;
		}
		if(j == 0 && fIsMerge == 0){
			if(qty < sum){
				swal_Tips("加工数量不能大于派工数量，注：加工数量=良品数+工废数+料废数+工废1+返修数+次品数！");
				return false;
			}	
		}else if(j == 0){//首道工序加工数需要和派工数量相比较
			if(qty < sum){
				swal_Tips("第一道工序的加工数量必须等于派工数量，注：加工数量=良品数+工废数+料废数+工废1+返修数+次品数！");
				return false;
			}	
		}else{//非首道工序加工数需要和上一道工序的良品数与次品数之和比较
			var foreGood = parseFloat($("#fPassQty" + (j - 1)).val()) + parseFloat($("#fOthQty" + (j - 1)).val());//上一道工序的良品数
			if(foreGood != sum){
				swal_Tips("第" + (j + 1) + "道工序的加工数量必须等于上一道工序的良品数量与次品数之和，注：加工数量=良品数+工废数+料废数+工废1+返修数+次品数！");
				return false;
			}
		}		
	}
	if(ID == ""){
		swal_Tips("请选择一条完工数据！");
		return false;
	}
/*	if(isNaN(fPassQty)){
		swal_Tips("良品数量不能为空！");
		return false;
	}
	if(fPassQty == 0){
		swal_Tips("良品数不能为0！");
		return false;
	}*/
	/*if(sum < 1){
		swal_Tips("加工数量不能为0，注：加工数量=良品数+工废数+料废数+丢失数+返修数+其他数！");
		return false;
	}
	if(qty < sum){
		swal_Tips("加工数量不能大于派工数量，注：加工数量=良品数+工废数+料废数+丢失数+返修数+其他数！");
		return false;
	}*/
	var list = getInputJson();
	if(list.length > 1){
		$("#mergeModal").modal('hide');	
	}else{
		$("#contentId_single").html("");
		$("#content_bottom_single").css("display","none");
	}
	showLoading();	
	$.ajax({
		url : pathUrl("end/mergeOperEndWork.action"),
		type : "POST",
		dataType : "JSON",
		data :{
			datas : JSON.stringify(list),
			fDisPathId : fDisPathId
		},
		success : function(data) {
			if(data.code == "0000"){
				swal_Tips_Short(data.message);
				flag = true;
				initFinishTableWithCurrentPage(currentPageNumber);
				$('.success').removeClass('success');
				rowIndex = "%^%%%%";
				if(data.type=='print'){
					url=pathUrl("WR?endId="+data.id);
					window.open(url);
				}
			}else{
				swal_Tips(data.message);
			}
		},
		error : function(request) {
			swal_Tips_Error();
		}
	});
});

/*
 * @Description: 实现修改上一行的非良品数，下面的良品数可以自动改变
 * @param : e
 */
function changeVal(e){
	var a = e.id;	
	if(!isNaN(e.value) && e.value != ""){
		var flag = false;
		var i = 1;
		for(; i < input_id.length; i++){
			var index = a.indexOf(input_id[i]);
			if(index == 0){
				flag = true;
				break;
			}
		}		
		if(flag){
			var temp = a.substr(input_id[i].length,a.length);//获得下标
			var sum = parseFloat($("#fPassQty" + temp).val()) + parseFloat($("#fOthQty" + temp).val());//当前行的良品数和次品数之和									
			for(var j = parseInt(temp) + 1; j < input_sum; j++){
				$("#fPassQty" + j).val(sum);
			}
		}
	}	
} 
/*
 * @Description:form表单转化成json字符串
 * @param :
 */
function getInputJson(){
	var json = [];
	for(var i = 0; i < input_sum; i++){
		var params = new Object();
		var sum = 0;
		params["fTargetQty"] = $("#fTargetQty").val();
		if(input_sum == 1){
			params["fStartWorkId"] = $("#fStartWorkId").val();
		}
		for(var j = 0; j < input_id.length; j++){			
			params[input_id[j]] = $("#" + input_id[j] + i).val();
			if(params[input_id[j]] == ""){
            	params[input_id[j]] = 0;
			}
			if(j > 0){
				sum += parseFloat($("#" + input_id[j] + i).val());
			}
		}
		if(fIsMerge == 1){//合并工序添加fMergeCode字段
			params["fMergeCode"] = $("#fMergeCode" + i).text() ;
			params["fOperSN"] = $("#fOperSN" + i).text();
			params["fCardId"] = $("#fCardId" + i).text();
			params["fQty"] = sum;
		}
		json.push(params);
	}
	return json;
}

/*
 * @Description: 重置按钮单击事件
 * @param :
 */
$("#reset").click(function() {
	flag = true;
	$(".clean-c").val("");
	$("#mergeModal").modal('hide');
	reseting();	
});
/*
 * @Description: 一条记录时的保存按钮
 * @param :
 */
$("#save_single").click(function(){	
	$("#save").click();
});
/*
 * @Description: 一条记录时的重置按钮单击事件
 * @param :
 */
$("#reset_single").click(function(){
	$("#contentId_single").html("");
	$("#content_bottom_single").css("display","none");
	flag = true;
	$(".clean-c").val("");	
	reseting();	
});
/*
 * @Description: 重置
 * @param :
 */
function reseting(){
	wipeData();
//	$("#save").attr("disabled", true);	
}

/*
 * @Description: 输入操作工姓名/操作工ID/派工单号回车事件
 * @param :
 */
$("#fWorkerF").keypress(function(event){
	if(event.keyCode == "13"){
		$("#contentId_single").html("");
		$("#content_bottom_single").css("display","none");
		fWorker = $("#fWorkerF").val();
		if(fWorker == ""){
			swal_Tips("请输入操作工卡号！");
			$(".clean-c").val("");
			fWorker = "$#^$%%^$%^$%";
			initFinishTable();
			$('.success').removeClass('success');
			rowIndex = "%^%%%%";
			$("#contentId").html("");
			$("#content_bottom").css("display","none");
//			$("#fPassQty,#fScrapQty,#fForitemQty,#fQtyLost,#fReworkQty,#fOthQty,#fWorker,#fStartWorkId,#fTargetQty").val("");
		}else if(fWorker.substring(0,2) == "PG" || fWorker.substring(0,3) == "MPG"){
			initFinishTable();
			$("#wname").val("");
			$('.success').removeClass('success');
			rowIndex = "%^%%%%";
		}else{
			flag = false;
			wipeData();
			$("#fPassQty").val("");
		}
	}
});
/*
 * @Description: 输入操作工姓名/操作工ID/派工单号回车事件
 * @param :
 */
function wipeData(){
	$("#contentId").html("");
	$("#content_bottom").css("display","none");
	$.ajax({
		url : pathUrl("emp/findByCondition.action"),
		type : 'GET',
		dataType : "JSON",
		data : {
			"row" : 100000,
			"fNumber" : $("#fWorkerF").val()
		},
		success : function(data) {
//			console.log(data)
			if(data.code == "0000"){
				if(data.rows.length == 1){
					$("#wname").val(data.rows[0].fName);
				}else{
					if(!flag){
						swal_Tips('请输入正确的操作工卡号！');
						$(".clean-c").val("");
					}
//					$("#wname").val("");
				}
			}else{
				swal_Tips('请输入正确的操作工卡号！');
				$(".clean-c").val("");
//				$("#wname").val("");
//				$("#fWorker").focus();
			}
			initFinishTable();
			$('.success').removeClass('success');
			rowIndex = "%^%%%%";			
//			$("#fPassQty,#fScrapQty,#fForitemQty,#fQtyLost,#fReworkQty,#fOthQty,#fWorker,#fStartWorkId,#fTargetQty").val("");
		},
		error : function(request) {
			swal_Tips_Error();
		}
	});
}
/*
 * @Description: 操作事件
 * @param :
 */
window.operateEvents= {
	'click .ggxh' : function(e, value, row, index) {//规格型号列点击
//		window.open(pathUrl("DLK?fNumber=")+row.fNumber);//传入参数
		fNumberData = row.fNumber;
		fOperSNData = row.fOperSN;
		initListTable();
		$("#showPDFModal").modal('show');
	},
	'click .TZName' : function(e, value, row, index) {//图纸名称列点击
		window.open(pathUrl("DLK?fName=")+row.fName);//传入参数
//		window.open(pathUrl("DLK?fName=1QJM12-01A3-30-3后盖工艺_1.pdf"));//传入参数
	}
}